package com.phkj.qdh.ui.fragment.usecar

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.commit
import androidx.lifecycle.ViewModelProvider
import com.hjq.toast.ToastUtils
import com.phkj.qdh.R
import com.phkj.qdh.bean.UseCarBean
import com.phkj.qdh.databinding.FragmentUseCarApprovalBinding
import com.phkj.qdh.ui.dialog.CommonConfirmDialog
import com.phkj.qdh.ui.dialog.CommonConfirmListener
import com.phkj.qdh.utils.gone
import com.phkj.qdh.utils.launchAndRepeatWithViewLifecycle
import com.phkj.qdh.utils.visible
import kotlinx.coroutines.flow.collectLatest

private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * 用车审批详情
 */
class UseCarApprovalFragment : Fragment() {
    private var param1: UseCarBean? = null
    private var param2: String? = null
    private var _binding: FragmentUseCarApprovalBinding? = null
    private val binding get() = _binding!!
    private lateinit var vm: CarVM
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getParcelable(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
        requireActivity().onBackPressedDispatcher.addCallback(this,
            object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {
                    parentFragmentManager.popBackStack()
                }
            })
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentUseCarApprovalBinding.inflate(inflater, container, false)
        vm = ViewModelProvider(this, getCarVMProvider())[CarVM::class.java]
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        param1?.let {
            binding.run {
                tvCar.text =
                    it.ysNameList.toString().replace(",", "、").replace("[", "").replace("]", "")
                tvStartDate.text = it.startTimeStr
                tvEndDate.text = it.endTimeStr
                edtReson.text = it.applyRemark
//                tvName.text = it.approverNameList.toString().replace(",", "、").replace("[", "")
//                    .replace("]", "")
            }
            when (it.applyStatus) {
                0 -> {
                    binding.tvNo.visible()
                    binding.tvOk.visible()
                    binding.tvState.gone()
                }
                1->{
                    binding.tvNo.gone()
                    binding.tvOk.gone()
                    binding.tvState.visible()
                    binding.tvState.text = "已同意"

                }
                2->{
                    binding.tvNo.gone()
                    binding.tvOk.gone()
                    binding.tvState.visible()
                    binding.tvState.text = "已拒绝"
                    binding.tvState.setBackgroundColor(ContextCompat.getColor(requireContext(),R.color.refuse))

                }
                else -> {
                    binding.tvNo.gone()
                    binding.tvOk.gone()
                    binding.tvState.visible()
                }
            }
        }

        binding.run {
            ivBack.setOnClickListener {
                parentFragmentManager.popBackStack(
                    "my_detail", FragmentManager.POP_BACK_STACK_INCLUSIVE
                )
            }
            tvOk.setOnClickListener {

                val dialog = CommonConfirmDialog.getInstance("同意用车申请", "确定同意吗")
                dialog.setCancelListener(object : CommonConfirmListener {
                    override fun dOk() {
                        submit(1)
                    }

                    override fun dCancel() {
                        dialog.dismiss()
                    }

                })
                dialog.show(parentFragmentManager, "")
            }
            tvNo.setOnClickListener {
                val dialog = CommonConfirmDialog.getInstance("拒绝用车申请", "确定要拒绝吗")
                dialog.setCancelListener(object : CommonConfirmListener {
                    override fun dOk() {
                        submit(2)
                    }

                    override fun dCancel() {
                        dialog.dismiss()
                    }

                })
                dialog.show(parentFragmentManager, "")
            }
        }

        launchAndRepeatWithViewLifecycle {
            vm.carUseApproval.collectLatest {
                if (it == 0) {
                    ToastUtils.show("审批成功")
                    parentFragmentManager.popBackStack("my_approval",FragmentManager.POP_BACK_STACK_INCLUSIVE)
                }
            }
        }
    }

    private fun submit(i: Int) {
        val map = HashMap<String, Any?>()
        map["applyId"] = param1?.applyId
        map["processType"] = i
        vm.carUseApproval(map)
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: UseCarBean, param2: String) = UseCarApprovalFragment().apply {
            arguments = Bundle().apply {
                putParcelable(ARG_PARAM1, param1)
                putString(ARG_PARAM2, param2)
            }
        }
    }
}